package com.aaa.member.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.member.dao.TbMemberDao;
import com.aaa.member.entity.TbMember;
import com.aaa.member.service.TbMemberService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TbMember)表服务实现类
 *
 * @author LiangLei
 * @since 2020-11-18 18:37:02
 */
@Service("tbMemberService")
public class TbMemberServiceImpl implements TbMemberService {
    @Resource
    private TbMemberDao tbMemberDao;
    
    /**
     *  分页加各种条件查询
     */
    @Override
    public Result queryPageByParam(Integer pageNo, Integer pageSize, TbMember tbMember) {
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //分页
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<TbMember> pageInfo = new PageInfo<TbMember>(tbMemberDao.queryAll(tbMember));
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),pageInfo);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TbMember queryById(Integer id) {
        return this.tbMemberDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<TbMember> queryAllByLimit(int offset, int limit) {
        return this.tbMemberDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param tbMember 实例对象
     * @return 实例对象
     */
    @Override
    public TbMember insert(TbMember tbMember) {
        this.tbMemberDao.insert(tbMember);
        return tbMember;
    }

    /**
     * 修改数据
     *
     * @param tbMember 实例对象
     * @return 实例对象
     */
    @Override
    public TbMember update(TbMember tbMember) {
        this.tbMemberDao.update(tbMember);
        return this.queryById(tbMember.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tbMemberDao.deleteById(id) > 0;
    }

    /**
     *  通过手机号查找是否存在
     */
    @Override
    public Result selectByPhone(String phoneNum) {
        List<TbMember> member = this.tbMemberDao.selectByPhone(phoneNum);
        if (member != null && member.size()>0) {
            return new Result(ReturnCode.FAIL.getCode(),
                    ReturnCode.FAIL.getMsg(),"手机号已存在");
        }else {
            return new Result(ReturnCode.SUCCESSS.getCode(),
                    ReturnCode.SUCCESSS.getMsg(),"不存在！");
        }

    }

    /**
     *  通过手机号修改密码
     */
    @Override
    public int updateByPhone(String password, String phoneNum){
        return this.tbMemberDao.updateByPhone(password,phoneNum);
    }

    @Override
    public TbMember queryMemberByPhone(String phoneNum) {
        TbMember member = new TbMember();
        member.setPhoneNum(phoneNum);
        List<TbMember> members = tbMemberDao.queryAll(member);
        if(members != null && members.size() > 0){
            return members.get(0);
        }
        return null;
    }

    @Override
    public Result memberLogin(String phoneNum, String passWord) {
        // 收集用户信息
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(phoneNum, passWord);
        //获取主体对象
        Subject currentUser = SecurityUtils.getSubject();
        try {
            //登录（执行认证过程）
            currentUser.login(usernamePasswordToken);
            //成功后操作
            TbMember member =  (TbMember) currentUser.getPrincipal();
            //生成token
            //String token = UUID.randomUUID().toString();
            //String token = phone+System.currentTimeMillis();
            //放入redis
            //redisTemplate.opsForValue().set(token,user,30, TimeUnit.HOURS);
            return new Result(ReturnCode.SUCCESSS.getCode(),
                    ReturnCode.SUCCESSS.getMsg(),"登陆成功！");
        }  catch (AuthenticationException e) {
            e.printStackTrace();
        }
        return new Result(ExceptionConstant.ERROR_USERNAME_PASSWORD.getErrorCode(),
                ExceptionConstant.ERROR_USERNAME_PASSWORD.getErrorMessage(),"用户名或密码错误");
    }

    @Override
    public List<TbMember> queryAll(TbMember tbMember) {
        return this.tbMemberDao.queryAll(tbMember);
    }

    @Override
    public int insertBy(TbMember tbMember) {
        return this.tbMemberDao.insertBy(tbMember);
    }

}